import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/business_customer/page/business_customer_details_service_sms.dart';

class BusinessClerkDetailsHeader extends StatelessWidget {
  final String userId;

  const BusinessClerkDetailsHeader({Key key, @required this.userId}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseScreenRefreshComponent(
        request: BaseScreenRequestModel('/x3-service-mobile-center/app/api/workbench/guide/detail/{guideId}', method: BaseHttpMethodEnum.GET, params: {'guideId': userId}),
        builder: (BaseHttpResultModel result) {
          if (!(result.data is Map)) return BaseEmptyComponent();
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildSummary(context, result.data),
              buildContactTool(context, result.data),
              SizedBox(height: 12),
            ],
          );
        });
  }

  Widget buildSummary(BuildContext context, Map<String, dynamic> detailMap) {
    return BaseCardComponent(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          buildClerkDetailsInfo(detailMap),
          SizedBox(height: 12.0),
          buildItemList(context, detailMap),
        ],
      ),
    );
  }

  Widget buildContactTool(BuildContext context, Map<String, dynamic> detailMap) {
    String guidePhone = BaseUtils.getKeyData(detailMap, 'guidePhone');
    return BaseCardComponent(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          buildToolItem(desc: '打电话', icon: 'https://glsfile.s3.cn-northwest-1.amazonaws.com.cn/gls-icon/Call.png', operate: () => onCallButton(phoneNO: guidePhone)),
          buildToolItem(desc: '发短信', icon: 'https://glsfile.s3.cn-northwest-1.amazonaws.com.cn/gls-icon/SMS.png', operate: () => onSMSButton(phoneNO: guidePhone)),
        ],
      ),
    );
  }

  Widget buildToolItem({@required String desc, @required String icon, @required Function() operate}) {
    return InkWell(
      onTap: operate,
      child: Column(
        children: [
          BaseImageComponent(imageUrl: icon, width: 40, height: 40),
          SizedBox(height: 8.0),
          Text(desc),
        ],
      ),
    );
  }

  Widget buildClerkDetailsInfo(Map<String, dynamic> detailMap) {
    String guideImg = BaseUtils.getKeyData(detailMap, 'guideImg');
    String guideName = BaseUtils.getKeyData(detailMap, 'guideName');
    String onJob = BaseUtils.getKeyData(detailMap, 'onJob').toString();
    return Row(
      children: [
        buildAvatar(avatarUrl: guideImg, userName: guideName),
        SizedBox(width: 12.0),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  buildUserName(userName: guideName),
                  SizedBox(width: 12.0),
                  buildLabel(onJob),
                ],
              ),
              // buildRating(detailMap),
            ],
          ),
        ),
      ],
    );
  }

  /// 头像
  Widget buildAvatar({@required String avatarUrl, @required String userName}) {
    return BaseImageComponent(imageUrl: avatarUrl, placeholderName: userName, size: BaseImageSizeEnum.original, type: BaseImageTypeEnum.X3User, width: 56, height: 56, circular: 56);
  }

  /// 用户名称
  Widget buildUserName({@required String userName}) {
    return Text(userName, style: TextStyle(color: Color(0xFF3333333), fontSize: 15));
  }

  /// 标签
  Widget buildLabel(String onJob) {
    if (onJob.isEmpty) return SizedBox();
    return Container(
      padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(2), border: Border.all(color: Color(0xFF5E96F4), width: 1)),
      child: Text(onJob == 'true' ? '在职' : '离职', style: TextStyle(fontSize: 12, color: Color(0xFF5E96F4))),
    );
  }

  /// 得分
  Widget buildRating(Map<String, dynamic> detailMap) {
    double guideStar = BaseDefaultsValue.doubleDefaults(BaseUtils.getKeyData(detailMap, 'guideStar', defaultValue: '0.0'));
    return BaseRatingBar(initValue: guideStar, iconSize: 18);
  }

  Widget buildItemList(BuildContext context, Map<String, dynamic> detailMap) {
    String totalSales = BaseUtils.getKeyData(detailMap, 'totalSales');
    String totalBills = BaseUtils.getKeyData(detailMap, 'totalBills');
    String exclusiveMember = BaseUtils.getKeyData(detailMap, 'exclusiveMember');
    String totalNew = BaseUtils.getKeyData(detailMap, 'totalNew');
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        buildItem(context, desc: '累计销售额', detail: totalSales),
        buildItem(context, desc: '累计开单', detail: totalBills),
        buildItem(context, desc: '专属会员', detail: exclusiveMember),
        buildItem(context, desc: '累计拉新', detail: totalNew),
      ],
    );
  }

  Widget buildItem(BuildContext context, {@required String desc, @required String detail}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(detail, style: BaseStyles.bodyMedium(context)),
        SizedBox(height: 4),
        Text(desc, style: BaseStyles.subtitle2(context)),
      ],
    );
  }

  // 打电话
  void onCallButton({@required String phoneNO}) {
    launch('tel:$phoneNO');
  }

  // 发短信
  void onSMSButton({@required String phoneNO}) {
    Map<String, dynamic> params = {'title': "请选择内容模版", 'inputApi': "", 'vipId': this.userId, 'value': phoneNO, 'sms': true, 'privateWx': true, 'enterpriseWx': true};
    BaseRouterUtil.getInstance().pushNamed(BusinessCustomerDetailsServiceSms.sName, arguments: params);
  }
}
